﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Kristall.Engine;
using Microsoft.Xna.Framework.Graphics;

namespace Kristall.MyGame.Enemies
{
    [Serializable]
    public class TipNPC : NPC
    {
        public string Tip { get; set; }
        AnimatableSprite idleAnimation;

        [XmlIgnore]
        public Level _level;

        float tipAlpha = 0; 

        public float TipAlpha { get { return tipAlpha; } set { tipAlpha = MathHelper.Clamp(value, 0, 1); } }
        float timer = 0;

        [XmlIgnore]
        Light _light;

        float tipRange = 500f;

        public TipNPC()
            : base()
        {
            this.GravityAcceleration = 0;
            Tip = "Hello!";
            this.CheckPlayerCollision = false;
            this.CheckWorldCollision = false;
        }

        public override void LoadContent(Level level)
        {
            _level = level;

            string idle = @"Enemies\Fairy.xnb";
            idleAnimation = new AnimatableSprite(idle);

            Animations.Clear();
            Animations.Add(idleAnimation);
            CurrentAnimationKey = idle;

            base.LoadContent(level);

            idleAnimation.FrameCount = 2;
            idleAnimation.FrameTime = 0.05f;
            idleAnimation.FrameHeight = idleAnimation.SpriteTextures[idleAnimation.FileName].Height;
            SetSize(idleAnimation.FrameWidth, idleAnimation.FrameHeight);

            _light = new Light();
            _light.Width = _light.Height = 100;
            _light.Center = this.Center;
            Level.DynamicLights.Add(_light);
        }

        public override void RemoveFromLevel()
        {
            base.RemoveFromLevel();
            Level.DynamicLights.Remove(_light);
        }

        public override void AddToLevel()
        {
            base.AddToLevel();
            Level.DynamicLights.Remove(_light);
        }

        public override void Update(float dt)
        {
            timer += dt * 3f;

            this.Velocity = new Vector2((float)Math.Cos(timer), (float)Math.Sin(timer)) * 150f;

            if (Vector2.Distance(this.Position, _level.Player.Position) < tipRange)
            {
                this.TipAlpha += dt;
            }
            else
            {
                this.TipAlpha -= dt;
            }

            base.Update(dt);

            if (IsAlive)
            {
                _light.Center = this.Center;
            }
            else
            {
                RemoveFromLevel();
            }
        }

        public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch batch)
        {
            base.Draw(batch);

            batch.DrawString(Kristall.SmallFont, Tip, this.Position + this.Size * Vector2.UnitX + Vector2.UnitX * 20f + Vector2.One, Color.Black*TipAlpha, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0.1f);
            batch.DrawString(Kristall.SmallFont, Tip, this.Position + this.Size * Vector2.UnitX + Vector2.UnitX * 20f, Color.White*TipAlpha, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
        }
    }
}
